package com.mb.app.controller;

import com.mb.app.domain.TbAppInfo;
import com.mb.app.domain.TbChannelConfig;
import com.mb.app.domain.TbUserClickLog;
import com.mb.app.service.ITbAppChannelConfigService;
import com.mb.app.service.ITbAppInfoService;
import com.mb.app.service.ITbChannelConfigService;
import com.mb.app.service.ITbUserClickLogService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwl
 * @program
 * @description 用户点击统计
 * @date 2019-03-26 11:33
 **/
@Controller
@RequestMapping("/app/userClick")
@Slf4j
public class UserClickController extends BaseController {


    @Autowired
    private ITbAppInfoService appInfoService;

    @Autowired
    private ITbUserClickLogService userClickLogService;

    @Autowired
    private ITbAppChannelConfigService appChannelConfigService;

    @Autowired
    private ITbChannelConfigService channelConfigService;

    @GetMapping("/index")
    String index(Model model) {
        // 查询应用信息
        Map<String, Object> map = new HashMap<String, Object>();
        List<TbAppInfo> appInfos = appInfoService.getAppInfoList(map);
        model.addAttribute("appInfos", appInfos);
        // 查询渠道信息
        List<TbChannelConfig> appChannelConfigs = channelConfigService.queryList(map);
        model.addAttribute("appChannelConfigs", appChannelConfigs);
        return "/app/userClick/userClick_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String clickTime = (String) params.get("clickTime");
        if (StringUtils.isNotEmpty(clickTime)) {
            params.put("startTime", DateUtils.parse(clickTime.substring(0, 10),DateUtils.DATE_PATTERN));
            params.put("endTime", clickTime.substring(13, 23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbUserClickLog> list = userClickLogService.queryList(query);
        int total = userClickLogService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @ResponseBody
    @RequestMapping(value = "/exportExcel")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {

        String clickTime = (String) params.get("clickTime");
        if (StringUtils.isNotEmpty(clickTime)) {
            params.put("startTime", DateUtils.parse(clickTime.substring(0, 10),DateUtils.DATE_PATTERN));
            params.put("endTime", clickTime.substring(13, 23)+" 23:59:59");
        }
        userClickLogService.exprot(response,params);
    }

}
